package com.atguigu.test.processor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Objects;
import java.util.Properties;

/**
 * @author zp
 * @version 1.0
 * @description: 自定义bean工厂增强器
 * BeanFactoryPostProcessor是增强bean工厂的接口
 * 实现在bean工厂 在加载配置之后的功能增强 BeanPostProcessor是有区别的
 * 可以在工厂层面 增强所有想通过工厂生成类的bean对象
 * <p>
 * BeanDefinition是对于 BeanFactory创类的具体描述
 * 对应了<bean></bean> 标签的属性 我们可以从这里获取到生成的所有定义信息
 * @date 2022/11/28 22:00
 */
@Component
@Slf4j
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        log.info("BeanFactoryPostProcessor 》》》开始执行了");
        String[] names = beanFactory.getBeanDefinitionNames();
        for (String name : names) {
            if ("user".equals(name)) {
                BeanDefinition beanDefinition = beanFactory.getBeanDefinition(name);
                MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
                // MutablePropertyValues如果设置了相关属性，可以修改，如果没有设置则可以添加相关属性信息
                if (propertyValues.contains("userName")) {
                    propertyValues.addPropertyValue("userName", "bobo");
                    log.info("BeanFactoryPostProcessor 》》》 user修改了属性信息");
                }
            }
        }

        if(beanFactory.containsBean("mySql")){
            BeanDefinition mySql = beanFactory.getBeanDefinition("mySql");
            MutablePropertyValues propertyValues = mySql.getPropertyValues();
            String configPath = (String)Objects.requireNonNull(propertyValues.getPropertyValue("configPath")).getValue();
            assert configPath != null;
            InputStream inputStream = this.getClass().getResourceAsStream(configPath);
            Properties properties = new Properties();
            try {
                properties.load(inputStream);
                properties.list(System.out);
                String url = properties.getProperty("jdbc.url");
                String driver = properties.getProperty("jdbc.driver");
                String username = properties.getProperty("jdbc.username");
                String password = properties.getProperty("jdbc.password");

                Connection connection = DriverManager.getConnection(url, username,password);
                beanFactory.registerSingleton("connection",connection);
                log.info("完成连接对象的初始化：[{}]",connection);
            } catch (IOException | SQLException e) {
                throw new RuntimeException(e);
            }
        }

        log.info("BeanFactoryPostProcessor 》》》 执行结束");
    }
}
